package com.ikdong.toodle.dao;

import java.util.ArrayList;
import java.util.List;
import com.ikdong.toodle.model.TaskContext;
import com.ikdong.toodle.util.Constant;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.util.Log;

public class ContextDAO {
	private final String TABLE = "Context";
	private DataBaseHelper db;
	
	public ContextDAO(Context context){
		db = new DataBaseHelper(context);
	}
	
	public boolean add(Long id, String name,Long privated,Long status,String sig){
		long rlt = -1;
		try{
			db.openDataBase();
			ContentValues values = new ContentValues();
			values.put("_id", id);
			values.put("name", name);
			values.put("private", privated);
			values.put("status", status);
			if(sig!=null) values.put("sig", sig);
			rlt = db.getWritableDatabase().insert(TABLE, null, values);	
		}catch(Exception e){
			Log.e(Constant.TAG, "ContextDAO.add() error", e);
		}finally{
			if(db!=null) db.close();
		}
		
		return rlt>-1;
	}
	
	public boolean delete(long id){
		long rlt = 0;
		try{
			db.openDataBase();
			String where = "_id='"+id+"'";
			rlt = db.getWritableDatabase().delete(TABLE, where,null);
		}catch(Exception e){
			Log.e(Constant.TAG, "ContextDAO.delete() error", e);
		}finally{
			if(db!=null) db.close();
		}
		
		return rlt>0;
	}
	
	public boolean deleteWithoutIds(List<Long> ids){
		long rlt = 0;
		try{
			String where = null;
			if(ids.size()>0){
				String dw = "";
				for(Long id : ids){
					dw += ","+id+"";
				}
				dw = dw.substring(1, dw.length());
				where = "_id not in ("+dw+")";
			}
			db.openDataBase();
			rlt = db.getWritableDatabase().delete(TABLE, where,null);
		}catch(Exception e){
			Log.e(Constant.TAG, "ContextDAO.deleteWithoutIds() error", e);
		}finally{
			if(db!=null) db.close();
		}
		
		return rlt>0;
	}
	
	public boolean update(TaskContext ctx){
		long rlt = 0;
		try{
			db.openDataBase();
			String where = "_id="+ctx.getId();
			ContentValues values = new ContentValues();
			if(ctx.getName()!=null)
				values.put("name", ctx.getName());
			if(ctx.getPrivated()!=null)
				values.put("private", ctx.getPrivated());
			if(ctx.getStatus()!=null)
				values.put("status", ctx.getStatus());
			rlt = db.getWritableDatabase().update(TABLE, values, where, null);
		}catch(Exception e){
			Log.e(Constant.TAG, "ContextDAO.update() error", e);
		}finally{
			if(db!=null) db.close();
		}
		
		return rlt>0;
	}
	
	public boolean replace(String id,TaskContext cxt){
		long rlt = 0;
		try{
			db.openDataBase();
			String where = "_id="+id;
			ContentValues values = new ContentValues();
			values.put("status", Constant.STATUS_SYNC_DONE);
			if(cxt.getId()!=null)
				values.put("_id", cxt.getId().toString());
			if(cxt.getName()!=null)
				values.put("name", cxt.getName());
			rlt = db.getWritableDatabase().update(TABLE, values, where, null);
		}catch(Exception e){
			Log.e(Constant.TAG, "ContextDAO.replace() error", e);
		}finally{
			if(db!=null) db.close();
		}
		
		return rlt>0;
	}
	
	public TaskContext get(long id){
		TaskContext cxt = null;
		Cursor cursor = null;
		try{
			db.openDataBase();
			String where = "_id='"+id+"'";
			cursor = db.getReadableDatabase().query(true, TABLE, TaskContext.getColumns(), where,null, null, null, null, null);
			if(cursor!=null && cursor.moveToFirst()){
				cxt = parse(cursor);
			}
		}catch(Exception e){
			Log.e(Constant.TAG, "ContextDAO.get(id) error", e);
		}finally{
			if(cursor!=null) cursor.close();
			if(db!=null) db.close();
		}
		
		return cxt;
	}
	
	public TaskContext get(String name){
		TaskContext cxt = null;
		Cursor cursor = null;
		try{
			db.openDataBase();
			String where = "name='"+name+"'";
			cursor = db.getReadableDatabase().query(true, TABLE, TaskContext.getColumns(), where,null, null, null, null, null);
			if(cursor!=null && cursor.moveToFirst()){
				cxt = parse(cursor);
			}
		}catch(Exception e){
			Log.e(Constant.TAG, "ContextDAO.get(name) error", e);
		}finally{
			if(cursor!=null) cursor.close();
			if(db!=null) db.close();
		}
		
		return cxt;
	}
	
	public List<TaskContext> get(String sig,long... statusLst){
		List<TaskContext> cxts = new ArrayList<TaskContext>();
		Cursor cursor = null;
		try{
			db.openDataBase();			
			String where = "sig='"+sig+"' ";			
			if(statusLst!=null){
				where +=" and status in (-1";
				for(long status : statusLst){
					where += " ,"+status;
				}
				where +=" ) ";
			}
			cursor = db.getReadableDatabase().query(true, TABLE, TaskContext.getColumns(), where,null, null, null, null, null);
			if(cursor!=null && cursor.moveToFirst()){
				do{
					cxts.add(parse(cursor));
				}while(cursor.moveToNext());
			}
		}catch(Exception e){
			Log.e(Constant.TAG, "ContextDAO.get(long... statusLst) error", e);
		}finally{
			if(cursor!=null) cursor.close();
			if(db!=null) db.close();
		}
		
		return cxts;
	}
	
	public boolean updateStatus(long id, long status){
		long rlt = 0;
		try{
			db.openDataBase();			
			String where = "_id='"+id+"'";
			ContentValues values = new ContentValues();
			values.put("status", status);
			rlt = db.getWritableDatabase().update(TABLE, values, where, null);
		}catch(Exception e){
			Log.e(Constant.TAG, "ContextDAO.updateStatus error", e);
		}finally{
			if(db!=null) db.close();
		}
		
		return rlt>0;
	}
	
	private TaskContext parse(Cursor cursor){
		TaskContext cxt = new TaskContext();
		cxt.setId(cursor.getLong(0));
		cxt.setName(cursor.getString(1));
		cxt.setPrivated(cursor.getLong(2));
		cxt.setStatus(cursor.getLong(3));
		cxt.setSig(cursor.getString(4));
		return cxt;
	}	
}
